Advertisement:

Author Topic: [SOLVED] How to change the position of a single custom field in iterm view  (Read 2751 times)

bernascoginger

  • Newbie
  • *
  • Posts: 30
hey buddies,
 
I am an amateur in programming, but i have successfully installed the osclass open classifiied software.
I want to edit the "Publish your ad" form to include "Phone no:" .

 I want my users(publishers or sellers) to be able to add their phone numbers to their items posted so that they can be contacted directly by buyers.
Please can any body help me edit the bender theme to include "phone no:"
« Last Edit: October 11, 2014, 12:21:47 am by bernascoginger »

teseo

  • Hero Member
  • *****
  • Posts: 6169
Hi,

You can do it with a custom field. Admin Dashboard, Listing -> Custom fields.

Regards

bernascoginger

  • Newbie
  • *
  • Posts: 30
Hi teseo,
Thanks for your reply.
 
Am at custom field, kindly give me a thorough procedure of how this can be don't
inorder to add a "phone number " field to my sellers


bernascoginger

  • Newbie
  • *
  • Posts: 30
Hi teseo,
 Thank you very much, it worked. But Can I display it as part of "contact publisher" sidebar after the "name " of publisher?.

Kindly regards.

teseo

  • Hero Member
  • *****
  • Posts: 6169
What theme are you using and what is the Identifier Name of your custom field?
 

bernascoginger

  • Newbie
  • *
  • Posts: 30
I installed osclass 3.3.2. I am using the default theme, Bender.
The identifier name is "Phone".

teseo

  • Hero Member
  • *****
  • Posts: 6169
Insert this at the bottom of your Bender item.php, just above the last line <?php osc_current_web_theme_path('footer.php') ; ?>

Code: [Select]
<script type="text/javascript">
    $(document).ready(function(){
        phone = $(".meta strong:contains('Phone')");

        $("p.name").append((phone.parents('.meta').css('padding-top', '5px')));
        phone.css('font-weight', 'normal');
    });
</script>

Regards

bernascoginger

  • Newbie
  • *
  • Posts: 30
Teseo,

It worked perfectly. thanks buddy.
I guess my programming skills will perfect easily
with you.

I will Love to know how all this happened,infact the mechanism behind that.

Regards.

teseo

  • Hero Member
  • *****
  • Posts: 6169
It's very time-consuming to deconstruct code but well, only for this time... :P

Code: [Select]
<script type="text/javascript">
    $(document).ready(function(){
       
// The code inside this function will be executed only when the whole page is loaded. We need to make sure the elements we are interested in already exist, otherwise our code would just throw a Javascript error.
    });
</script>

The function itself:

We want to move a given element from its original location to a new one (after Name in Contact form). So, we analyze the source code of the page using the developer tools that any modern browser includes. We need to know the unique selector for the two elements involved here so we may operate on them.

Request page of any Item using Bender (I'm going to use FireFox here, but it's not so different with other browsers).

Name in Contact form:

Right mouse button over it, "Inspect Element" and the Console shows that part of the HTML code. We can see that is a link (a) enclosed in a paragraph (p):

Code: [Select]
<p class="name">Name: <a href="http://mysite.com/user/profile/John">John</a></p>
This paragraph doesn't have an ID to give it full uniqueness, but it does have a class ("name"), that seems no other element is using in the page, so "paragraph having class 'name'" is enough:

p.name

But hey, the console can tell us this on its own, RMB over <p class="name"> in the code (or over the highlighted label "p" above -BTW, look there it is the whole tree of the element, might be useful for other tasks, yay! :D) and select "Copy unique selector", the result:

.name

Why, seems that after all here we wouldn't even need to put "p" (because it's the only element that is using that class, but well, it's only a letter and in case one day we'd want to go back over this, it might be easier to identify what's what, so we stick to p.name).

Now we know how to call the place where we want to move the other element after, so let's get to it.

Custom field "Phone":

Same procedure, Inspect element and get the unique selector:

.meta > strong:nth-child(1)

It's the first "strong" inside the only element that is using class "meta". Hm... What if at some point we'd want to put another custom field the first in the list of them? Then this code would move not "Phone" but the other one. We need something more specific here.  ???

Let's see, "strong" doesn't have nor ID nor a class, the only thing different to any other "strong" for other custom fields is its contents (the text "Phone:"). Here is where Jquery comes to the rescue: the method "contains",

.meta > strong:contains('Phone:')

...And this is all for now, I'll continue later or tomorrow. As I said, very time-consuming, have to run now... :D

Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Well, time for the second chapter... :D

Then, at this point we've identified the two elements we need to achieve our goal:

1.- p.name, destination point.

2.- .meta > strong:contains('Phone:'), this we need to move it after destination point.

However, still something to take into acount now: we don't want to move only the strong element, but the whole div where it is inserted. Maybe that div has a pretty style that we want for that custom field... ???

So, we need to identify that div. Again, no ID, only the class "meta", shared by all the custom fields. The solution is in another method of jQuery:

$(".meta > strong:contains('Phone:')").parents('.meta')

This is a possible selector for the div with class "meta" containing the strong we are interested in. parents() finds the immediate parent matching its contents.

And seems that now, finally, we are ready to make the moving:

Quote
$("p.name").append($(".meta > strong:contains('Phone:')").parents('.meta'));

Success! jQuery method "append" moves the custom field div after the paragraph with the name of the user.

Only problem now is that these two elements were never designed to appear side by side, so the look of it is a bit ugly. We'll fix that applying a couple of CSS changes:

First, we get rid of the bold style in Custom field name:

Quote
$(".meta > strong:contains('Phone:')").css('font-weight', 'normal');

and then we add a bit of separation between the paragraph "name" and the div ".meta".

Quote
$(".meta > strong:contains('Phone:')").parents('.meta').css('padding-top', '5px');

The whole code at this point:

Code: [Select]
<script type="text/javascript">
    $(document).ready(function(){

        $("p.name").[b]append[/b]($(".meta > strong:contains('Phone:')").parents('.meta'));
        $(".meta > strong:contains('Phone:')").css('font-weight', 'normal');
        $(".meta > strong:contains('Phone:')").parents('.meta').css('padding-top', '5px');

    });
</script>

And this is where you may tell yourself "mission accomplished" and go for the next headache on your list :D or you could choose to optimize the code so it would consume less time and resources of the user device.

Right now we have three ".meta > strong:contains('Phone:')". Why is this relevant? Because every time the interpreter in the browser finds a complicated selector like ".meta > strong:contains('Phone:')" it needs to look in the whole code for the elements that match it. So, the less, the better. ;)

We can assign the result of this complicated search to a variable and the process would be done just once:

Quote
var phone = $(".meta > strong:contains('Phone:')");

...use that variable as selector:

Code: [Select]
<script type="text/javascript">
    $(document).ready(function(){

        var phone = $(".meta > strong:contains('Phone:')");

        $("p.name").append(phone.parents('.meta'));
        phone.css('font-weight', 'normal');
        phone.parents('.meta').css('padding-top', '5px');
    });
</script>

Something more that might be optimized yet? It seems so: There is still a repeated selector:  phone.parents('.meta')

We could assign it to another variable again, but we can do it with jQuery, so:

Quote
$("p.name").append((phone.parents('.meta').css('padding-top', '5px')));

At the same time we are moving with "append" we take advantange and make the style change ("padding-top") to the div .meta.

The final code (at last :D):

Code: [Select]
<script type="text/javascript">
    $(document).ready(function(){
        var phone = $(".meta > strong:contains('phone:')");

        $("p.name").append((phone.parents('.meta').css('padding-top', '5px')));
        phone.css('font-weight', 'normal');
    });
</script>

Well, as promised, first and last time I do this... :D Hope it will be useful to people around. ;)

Regards
« Last Edit: October 03, 2014, 06:44:04 pm by teseo »

bernascoginger

  • Newbie
  • *
  • Posts: 30
Hi Teseo,
     
That's a great enlightenment from you.
You've improved my understanding on the DOM.
 
Thank you, Teseo.
You(Teseo) have made osclass forum an inspiring place to be.

Regards.

teseo

  • Hero Member
  • *****
  • Posts: 6169
You're so kind. :)

Just found some errors in my last post, corrected.

Could you change the title of this thread to something like "[SOLVED] How to change position of a single custom field in Item View") Thanks.

Regards